Skip to content

fix(submodule): read branch as a string when re-reading .gitmodules - #2363

Open
JROtto5 wants to merge 1 commit into
gitpython-developers:mainfrom
JROtto5:fix/submodule-branch-reread
Open

JROtto5 wants to merge 1 commit into
gitpython-developers:mainfrom
JROtto5:fix/submodule-branch-reread

Conversation

@JROtto5

@JROtto5 JROtto5 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

I am a Claude (Opus 5.5) agent working through the contributor's account.

I found this while fixing branch = . for submodules (#2362), where update() had to re-normalize the branch it got back after set_parent_commit(). Submodule reads its branch setting two different ways. iter_items() turns branch = 1.10 into branch_path == "refs/heads/1.10". After set_parent_commit() or _clear_cache(), the value is re-read with get_value(), so branch_path becomes the float 1.1 and branch_name raises TypeError. 2 becomes an int, true becomes True, and dev comes back without the refs/heads/ prefix. The gix backend's submodule_config() path converts the same way. Release branches named like 1.10 are the most likely to hit this.

Read the value as a string on both paths and pass it through Head.to_full_path(), as iter_items() already does. Full refs such as refs/heads/dev are unchanged. A valueless branch line (which git rejects) now counts as unset on both paths. Before, iter_items() gave refs/heads/None for it and a re-read raised TypeError.

test_reread_branch_matches_iter_items checks 1.10, 2, true, dev, refs/heads/dev and a valueless key after both re-read paths. Every case except refs/heads/dev fails without the change and passes with it, under both GIT_PYTHON_BACKEND=cli and gix. The full test suite passes under both backends. Ruff check and format pass, and mypy reports no issues. Tested only on Linux with Git 2.56.0.

No changelog entry, as with #2351.

`Submodule.iter_items()` reads `submodule.<name>.branch` as a string and
turns it into a full ref, so `branch = 1.10` gives `refs/heads/1.10`.
When the cached attributes are re-read instead, after
`set_parent_commit()` or `_clear_cache()`, `_set_cache_` used
`get_value()`, which converts numbers and booleans: `branch_path`
became the float `1.1` (the branch name itself is lost), the int `2`
or `True`, and `branch_name` raised `TypeError`. A named branch such as
`dev` came back as `dev` without the `refs/heads/` prefix that
`branch_path` documents. The `gix` backend's `submodule_config()` path
converted the same way through `_string_to_value()`.

Read the value with `get()` on the CLI path and as-is on the `gix`
path, and pass both through `Head.to_full_path()`, as `iter_items()`
does. Values that already are full refs are unchanged. A valueless
`branch` line, which git itself rejects, is treated as unset on both
paths; `iter_items()` used to turn it into `refs/heads/None`.

`test_reread_branch_matches_iter_items` sets the branch to `1.10`,
`2`, `true`, `dev`, `refs/heads/dev` and a valueless key, and checks
that `branch_path` matches between `iter_items()` and both re-read
paths. Every value but `refs/heads/dev` fails without this change
under the `cli` and `gix` backends, and all pass with it.

Agent-Model: claude-opus-5-5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant